﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ReportForm.BLL
{
    class tReportSell
    {
        UTLDB.UTLDB connect = new UTLDB.UTLDB();
        DataTable data = new DataTable();
        DataTable dataSQL = new DataTable();

        public tReportSell()
        { 
            DataColumn[] dc = new DataColumn[]{ 
                                new DataColumn("No", Type.GetType("System.Int32")),
                                   new DataColumn("Barcode", Type.GetType("System.String")),
                                  new DataColumn("Name", Type.GetType("System.String")),
                                 new DataColumn("Wgam", Type.GetType("System.Double")),
                                 new DataColumn("Wchi", Type.GetType("System.String")),
                                new DataColumn("Price", Type.GetType("System.Double")),
                                new DataColumn("Date", Type.GetType("System.String")),
                                 new DataColumn("Begin", Type.GetType("System.String")),
                                new DataColumn("TotalChi", Type.GetType("System.String")),
                                new DataColumn("End", Type.GetType("System.String"))
            };
            data.Columns.AddRange(dc);
        }
        public string GetTotalChi(DataTable data)
        {
            double temp = 0;
            for (int i = 0; i < data.Rows.Count; i++)
            {
                temp += double.Parse(data.Rows[i]["WChi"].ToString());
            }
            return FormatWeight(temp.ToString());
        }
        public DataTable GetSell(int idCat,string from, string to)
        {
            DataTable data = connect.GetDataByStoredProcedure("sp_Report_SelectProductSell",
               new string[] {"@IdCat", "@DateFrom", "@DateTo" },
               new object[] {idCat,to,from }
                );
            return data;
        }
        public DataTable SetData(int idCat,string from, string to)
        {
            string TotalChi = "";
            int i = 0;
            dataSQL = GetSell(idCat,from, to);
            TotalChi = GetTotalChi(dataSQL);
            for (i = 0; i < dataSQL.Rows.Count; i++)
            {
                DataRow r = data.NewRow();
                r["No"] = i + 1;

                r["Barcode"] = dataSQL.Rows[i]["Barcode"];
                r["Name"] = dataSQL.Rows[i]["NamePr"];
                r["Wgam"] = dataSQL.Rows[i]["WGam"];
                r["Wchi"] = FormatWeight(dataSQL.Rows[i]["WChi"].ToString());
                r["Price"] =  dataSQL.Rows[i]["Price"];
                r["Date"] = dataSQL.Rows[i]["DateSell"];
                r["begin"] = to;
                r["TotalChi"] = TotalChi;
                r["end"] = from;

                data.Rows.Add(r);
            }
            return data;
        }
        public string FormatWeight(string str)
        {
            string size = "";
            string[] tam = str.Trim().Split('.');
            string phanle;
            if (tam.Length > 1) phanle = tam[1];
            else phanle = "";

            switch (tam[0].Length)
            {
                case 1:
                    if (tam[0] == "0") size = 0 + phanle;
                    else size = tam[0] + "L" + phanle;
                    break;
                case 2:
                    size = tam[0].Substring(0, 1) + "P"
                        + tam[0].Substring(1, 1) + "L" + phanle;
                    break;
                case 3:
                    size = tam[0].Substring(0, 1) + "C"
                        + tam[0].Substring(1, 1) + "P" +
                        tam[0].Substring(2, 1) + "L" + phanle;
                    break;
                case 4:
                    size = tam[0].Substring(0, 1) + "L" +
                        tam[0].Substring(1, 1) + "C"
                        + tam[0].Substring(2, 1) + "P" +
                        tam[0].Substring(3, 1) + "L" + phanle;
                    break;

            }
            return size;
        } 

    }
}
